package tgc.edu.tms.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import tgc.edu.tms.custom.CommonService;
import tgc.edu.tms.entity.ClassSchedule;
import tgc.edu.tms.entity.Jcb;
import tgc.edu.tms.entity.SysUser;
import tgc.edu.tms.entity.TbClass;
import tgc.edu.tms.entity.Teacher;
import tgc.edu.tms.repository.ClassScheduleRepository;
import tgc.edu.tms.web.form.AgcsFrom;

@Service
public class ClassScheduleService extends CommonService<ClassSchedule, Integer> {
	@Autowired
	private ClassScheduleRepository classScheduleDAO;

	public List<ClassSchedule> findByEpTeacherUsername(String username) {
		return classScheduleDAO.findByEpTeacherUsername(username);
	}

	public List<ClassSchedule> findByJcbJcsjAndJcbXqAndEpTeacherUsername(String jc, String weekDay, String teacher) {
		return classScheduleDAO.findByJcbJcsjAndJcbXqAndEpTeacherUsername(jc,weekDay,teacher);
	}

	public List<ClassSchedule> findByJcbJcsjAndJcbXqAndClassroomSerialNumber(String section, String weekDay,
			String address) {
		
		return classScheduleDAO.findByJcbJcsjAndJcbXqAndClassroomSerialNumber(section,weekDay,address);
	}

	public List<ClassSchedule> findByJcbJcsjAndJcbXqAndEpCurriculumKcmc(String section, String weekDay2,
			String curriculum) {
		return classScheduleDAO.findByJcbJcsjAndJcbXqAndEpCurriculumKcmc(section,weekDay2,curriculum);
	}

	public List<ClassSchedule> findByJcbJcsjAndJcbXq(String jc, String weekDay2) {
		return classScheduleDAO.findByJcbJcsjAndJcbXq(jc,weekDay2);
	}

	public List<ClassSchedule> findByEpTeacherUsernameAndJcbJcsjAndJcbXq(String username, String section, String week) {
		return classScheduleDAO.findByEpTeacherUsernameAndJcbJcsjAndJcbXq(username,section,week);
	}

	public List<ClassSchedule> findByJcbXqAndClassroomLocationNameAndClassroomFloor(String week, String name,
			String floor) {
		return classScheduleDAO.findByJcbXqAndClassroomLocationNameAndClassroomFloor(week,name,floor);
	}
	/***
	 * 取出所有相同班级的节次表ID
	 * @param etbclass
	 * @param from
	 * @return
	 */
	public List<ClassSchedule> findByClasses(List<TbClass> etbclass, AgcsFrom from) {
		List<ClassSchedule> ClassSchedules =new ArrayList<>();
		Integer schoolyearID=from.getSchoolyearID();
		List<ClassSchedule> classSchedule = classScheduleDAO.findByEpSchoolyearId(schoolyearID);
		for (int i = 0; i < classSchedule.size(); i++) {
			ClassSchedule c = classSchedule.get(i);
			List<TbClass> tbclass = c.getTbclass();
			int sum=1;
			
			for (int j = 0; j < tbclass.size(); j++) {
				TbClass tbClass2 = tbclass.get(j);
				for (int k = 0; k < etbclass.size(); k++) {
					TbClass tbClass3 = etbclass.get(k);
					if(tbClass2.getId()==tbClass3.getId()) {
						ClassSchedules.add(c);
						 sum=0;
					}
					if(sum==0) {
						break;
					}
				}
				if(sum==0) {
					break;
				}
				
			}
			
		}
		return ClassSchedules;
	}

	public List<ClassSchedule> findByTeachers(Teacher teacher, AgcsFrom from) {
		List<ClassSchedule> ClassSche=classScheduleDAO.findByEpTeacherIdAndEpSchoolyearId(teacher.getId(),from.getSchoolyearID());	
		return ClassSche;
	}

	public Integer findMaxId() {
		Integer findMaxId = classScheduleDAO.findMaxId();
		if(findMaxId==null) {
			findMaxId=0;
		}
		return findMaxId;
	}

	public void deleteByScyearId(Integer schoolyearID) {
		List<ClassSchedule> fin = classScheduleDAO.findByEpSchoolyearId(schoolyearID);
		for (int i = 0; i < fin.size(); i++) {
			ClassSchedule classSchedule = fin.get(i);
			classScheduleDAO.deleteById(classSchedule.getId());
		}
	}

}
